package cn.iocoder.yudao.module.crossborder.srm.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.srm.api.dto.ContractDTO;
import cn.iocoder.yudao.module.crossborder.srm.api.dto.PurchaseOrderDTO;
import cn.iocoder.yudao.module.crossborder.srm.api.dto.SupplierCreateReqDTO;
import cn.iocoder.yudao.module.crossborder.srm.api.dto.SupplierDTO;
import cn.iocoder.yudao.module.crossborder.srm.service.SrmService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 供应商关系管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 供应商关系管理")
@RestController
@RequestMapping("/crossborder/srm")
@Validated
public class SrmController {

    @Resource
    private SrmService srmService;

    @GetMapping("/supplier/get")
    @Operation(summary = "获得供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:srm:query')")
    public CommonResult<SupplierDTO> getSupplier(@RequestParam("id") Long id) {
        SupplierDTO supplier = srmService.getSupplier(id);
        return success(supplier);
    }

    @GetMapping("/supplier/list")
    @Operation(summary = "获得供应商列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:srm:query')")
    public CommonResult<List<SupplierDTO>> getSupplierList(@RequestParam("ids") List<Long> ids) {
        List<SupplierDTO> suppliers = srmService.getSupplierList(ids);
        return success(suppliers);
    }

    @PostMapping("/supplier/create")
    @Operation(summary = "创建供应商")
    @PreAuthorize("@ss.hasPermission('crossborder:srm:create')")
    public CommonResult<Long> createSupplier(@RequestBody @Valid SupplierCreateReqDTO createReq) {
        Long id = srmService.createSupplier(createReq);
        return success(id);
    }

    @GetMapping("/purchase-order/get")
    @Operation(summary = "获得采购订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:srm:query')")
    public CommonResult<PurchaseOrderDTO> getPurchaseOrder(@RequestParam("id") Long id) {
        PurchaseOrderDTO purchaseOrder = srmService.getPurchaseOrder(id);
        return success(purchaseOrder);
    }

    @GetMapping("/contract/get")
    @Operation(summary = "获得合同")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:srm:query')")
    public CommonResult<ContractDTO> getContract(@RequestParam("id") Long id) {
        ContractDTO contract = srmService.getContract(id);
        return success(contract);
    }

    @PostMapping("/supplier/evaluate")
    @Operation(summary = "供应商评估")
    @Parameter(name = "supplierId", description = "供应商编号", required = true, example = "1024")
    @Parameter(name = "score", description = "评估分数", required = true, example = "85")
    @Parameter(name = "comment", description = "评估意见", required = true, example = "表现良好")
    @PreAuthorize("@ss.hasPermission('crossborder:srm:evaluate')")
    public CommonResult<Boolean> evaluateSupplier(@RequestParam("supplierId") Long supplierId,
                                                  @RequestParam("score") Integer score,
                                                  @RequestParam("comment") String comment) {
        Boolean result = srmService.evaluateSupplier(supplierId, score, comment);
        return success(result);
    }

    @PostMapping("/supplier/certify")
    @Operation(summary = "供应商认证")
    @Parameter(name = "supplierId", description = "供应商编号", required = true, example = "1024")
    @Parameter(name = "certificationType", description = "认证类型", required = true, example = "ISO9001")
    @PreAuthorize("@ss.hasPermission('crossborder:srm:certify')")
    public CommonResult<Boolean> certifySupplier(@RequestParam("supplierId") Long supplierId,
                                                @RequestParam("certificationType") String certificationType) {
        Boolean result = srmService.certifySupplier(supplierId, certificationType);
        return success(result);
    }

    @PostMapping("/quality/inspect")
    @Operation(summary = "质量检验")
    @Parameter(name = "purchaseOrderId", description = "采购订单编号", required = true, example = "1024")
    @Parameter(name = "result", description = "检验结果", required = true, example = "合格")
    @PreAuthorize("@ss.hasPermission('crossborder:srm:inspect')")
    public CommonResult<Boolean> qualityInspect(@RequestParam("purchaseOrderId") Long purchaseOrderId,
                                               @RequestParam("result") String result) {
        Boolean inspectResult = srmService.qualityInspect(purchaseOrderId, result);
        return success(inspectResult);
    }

}
